package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.guoguojie.dao.MessageDao;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.MessageAmount;
import cn.sumpu.app.guoguojie.domain.Ext.MessageExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MessageDaoImpl extends SqlSessionDaoSupport implements MessageDao {

    private final static Logger logger = LoggerFactory.getLogger(MessageDaoImpl.class);

    /**
     * 查询消息(根据条件map)
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#getMessageList(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<MessageExt> getMessageList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageList failure.", e);
            }
            throw new AppBusinessException("getMessageList failure.", e);
        }
    }
    /**
     * 查询消息总数(根据条件map)
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#getMessageListCount(java.util.Map)
     */
    @Override
    public int getMessageListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageListCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageListCount failure.", e);
            }
            throw new AppBusinessException("getMessageListCount failure.", e);
        }
    }
  /**
   * 删除消息
   * @param map
   * @throws AppBusinessException
   * @see cn.sumpu.app.guoguojie.dao.MessageDao#deleteMessageByMap(java.util.Map)
   */
    @Override
    public void deleteMessageByMap(Map<String, Object> map) throws AppBusinessException { 
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageByMap", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMessageByMap failure.", e);
            }
            throw new AppBusinessException("deleteMessageByMap failure.", e);
        }
    }
    
    /**
     * 增加新消息
     * @param message
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#insertMessage(cn.sumpu.app.guoguojie.domain.Message)
     */
    @Override
    public void insertMessage(Message message) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MessageMapper.insertMessage", message);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertMessage failure.", e);
            }
            throw new AppBusinessException("insertMessage failure.", e);
        }
    }
    /**
     * 消息详情
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#getMessage(java.util.Map)
     */
    @Override
    public Message getMessage(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Message) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessage",map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessage failure.", e);
            }
            throw new AppBusinessException("getMessage failure.", e);
        }
    }
    
    // 发送站内消息
    @Override
    public void addClientFeedback(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MessageMapper.addClientFeedback", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("addClientFeedback failure.", e);
            }
            throw new AppBusinessException("addClientFeedback failure.", e);
        }
    }
    /**
     * 未读消息
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#getMessageAmountByMap(java.util.Map)
     */
    @Override
    public MessageAmount getMessageAmountByMap(Map<String, Object> map) throws AppBusinessException {
        try {
            return (MessageAmount) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageAmountByMap",map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageAmount failure.", e);
            }
            throw new AppBusinessException("getMessageAmount failure.", e);
        }
    }
    /**
     * 站内信未读数量
     * @param map
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MessageDao#getMessageNoReadCount(java.util.Map)
     */
    @Override
    public int getMessageNoReadCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageNoReadCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageNoReadCount failure.", e);
            }
            throw new AppBusinessException("getMessageNoReadCount failure.", e);
        }
    }
    
    
    @Override
    public void updateMessageAmount(Map<String, Object> map) throws AppBusinessException { 
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MessageMapper.updateMessageAmount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("addClientFeedback failure.", e);
            }
            throw new AppBusinessException("addClientFeedback failure.", e);
        }
    }
    
    @Override
    public void updateMessage(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MessageMapper.updateMessage", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateMessage failure.", e);
            }
            throw new AppBusinessException("updateMessage failure.", e);
        }
    }
    
    @Override
    public void insertMessageAmount(MessageAmount messageAmount) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MessageMapper.insertMessageAmount", messageAmount);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("addClientFeedback failure.", e);
            }
            throw new AppBusinessException("addClientFeedback failure.", e);
        }
    }
    
    
    
    
    
    
    
    
    
  /*  @SuppressWarnings("unchecked")
    @Override
    public List<MessageExt> getMessageFriend(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageFriend", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageFriend failure.", e);
            }
            throw new AppBusinessException("getMessageFriend failure.", e);
        }
    }

    @Override
    public int getMessageFriendCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageFriendCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageFriendCount failure.", e);
            }
            throw new AppBusinessException("getMessageFriendCount failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MessageExt> getMessageInner(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageInner", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageFriend failure.", e);
            }
            throw new AppBusinessException("getMessageFriend failure.", e);
        }
    }

    @Override
    public int getMessageInnerCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageInnerCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMessageInnerCount failure.", e);
            }
            throw new AppBusinessException("getMessageInnerCount failure.", e);
        }
    }

  

   

    @Override
    public void deleteMessageByFamily(String family) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageByFamily", family);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMessageByMember failure.", e);
            }
            throw new AppBusinessException("deleteMessageByMember failure.", e);
        }
    }

   

  

    

    @Override
    public void deleteMessage(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessage", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMessage failure.", e);
            }
            throw new AppBusinessException("deleteMessage failure.", e);
        }
    }

   

    @SuppressWarnings("unchecked")
    @Override
    public List<MessageAmount> getMessageAmount(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageAmount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query failure.", e);
            }
            throw new AppBusinessException("query failure.", e);
        }
    }

   

   
    @Override
    public void deleteMessageById(String identifier) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageById", identifier);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMessageById failure.", e);
            }
            throw new AppBusinessException("deleteMessageById failure.", e);
        }
    }
    
   
    @Override
    public void deleteMessageByReplyId(String messageByReplyId) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageByReplyId", messageByReplyId);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMessageByReplyId failure.", e);
            }
            throw new AppBusinessException("deleteMessageByReplyId failure.", e);
        }
    }*/
}
